<?php

namespace App\Kuafu\Mybank\Model;

use App\Kuafu\Base\Model\BaseModel;
use App\Kuafu\Base\Model\Traits\KfSoftDeletes;
use App\Kuafu\Base\Model\Traits\KfUid;
use App\Kuafu\Pay\Model\PayStreamModel;
use App\Kuafu\Shop\Model\Shop;
use App\Remote\Mybank\Remote;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasOneThrough;

class MybankOrderWithholdRecordModel extends BaseModel
{
    use HasFactory, KfSoftDeletes, KfUid;

    protected $connection = 'mysql';
    protected $table      = "mybank_order_withhold_record";

    protected $fillable = [
        'id',
        'outTradeNo',
        'billRecordId',
        'batchNo',
        'orderNo',
        'operateNo',
        'scene',
        'totalAmount',
        'walletRecordId',
        'freezeAmount',
        'availableAmount',
        'status',
        'submitDateTime',
        'finishDateTime',
        'currency',
        'payerId',
        'payerType',
        'payeeId',
        'payeeType',
        'memo',
        'extInfo',
        'errorCode',
        'errorDesc',
        'createdAt',
        'updatedAt',
        'createdByUid',
        'updatedByUid',
        'isDeleted',
    ];

    const WITHHOLD_STATUS_DEFAULT = 0; // 未提交
    const WITHHOLD_STATUS_SUBMIT  = 1; // 提交申请
    const WITHHOLD_STATUS_SUCCESS = 2; // 成功
    const WITHHOLD_STATUS_FAIL    = 3; // 失败

    const WITHHOLD_STATUS_TEXT = [
        self::WITHHOLD_STATUS_DEFAULT => '未提交',
        self::WITHHOLD_STATUS_SUBMIT  => '提交申请',
        self::WITHHOLD_STATUS_SUCCESS => '成功',
        self::WITHHOLD_STATUS_FAIL    => '失败',
    ];

    const WITHHOLD_SCENE_FREEZE = 'PROTOCOL_WITHHOLD_SUPPORT_FREEZE';

    const WITHHOLD_MERCHANT = 'MERCHANT';// 商户 MERCHANT
    const WITHHOLD_PLATFORM = 'PLATFORM';// 平台
    const WITHHOLD_MEMBER   = 'MEMBER';// 免入驻支付付方类型 MEMBER

    /**
     * @return HasOne
     */
    public function billRecordRel(): HasOne
    {
        return $this->hasOne(MybankBillRecordModel::class, 'id', 'billRecordId');
    }

    public function orderWithholdMappings(): HasMany
    {
        return $this->hasMany(
            MybankOrderRecordWithholdMappingsModel::class,
            'orderWithholdRecordId'
        );
    }

    /**
     * @return HasOne
     */
    public function myPayStreamRel(): HasOne
    {
        return $this->hasOne(PayStreamModel::class, "thirdPayNo", "outTradeNo");
    }

    public function getInfo(string $outOrderNo)
    {
        return self::query()->where('outOrderNo', $outOrderNo)->first();
    }

    public function updateById(int $id, array $data): int
    {
        return self::query()->where('id', $id)->update($data);
    }
}
